﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using UIT.ScrumTool.DTO;
using UIT.ScrumTool.Service.Business;
using UIT.ScrumTool.Service.Utilities;

namespace UIT.ScrumTool.Service
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class wndMain : Window
    {
        public wndMain()
        {
            InitializeComponent();

            //ControlService uit = new ControlService();
            //uit.ProjectUserGetByProjectIdNoTeam(1);
            //TeamDto teamDto = new TeamDto();
            //teamDto.Name = "My Team 1";
            //teamDto.Description = "My team 1";
            //uit.TeamInsert(teamDto);
        }

        //thuoc tinh utilites khoi tao cac doi tuong
        SerivceWCF serviceWCF = new SerivceWCF();
        ConfigConnection configConnection=new ConfigConnection();
        private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value);

        #region Kiem Tra Gia Tri Nhap Vao
        private void txtPort_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            try
            {
                Convert.ToInt32(e.Text);
                if (txtPort.Text.Length > 5)
                    e.Handled = true;
            }
            catch
            {
                e.Handled = true;
            }
        }

        private void txtServicePort_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            try
            {
                Convert.ToInt32(e.Text);
                if (txtServicePort.Text.Length > 5)
                    e.Handled = true;
            }
            catch
            {
                e.Handled = true;
            }
        }
        #endregion

        #region Thao Tac Thuc Hien
        private void btnStartService_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string strHost = "http://" + txtHost.Text.Trim() + ":" + txtServicePort.Text.Trim();
                serviceWCF.RegisterEndPointAddress(strHost);
                btnStart.IsEnabled = false;
                btnStop.IsEnabled = true;
                lbStatic.Content = "Service running.....";
                progressBar.Minimum = 0;
                progressBar.Maximum = 3000;
                progressBar.Value = 0;
                double value = 0;
                UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(progressBar.SetValue);
                do
                {
                    value += 1;
                    Dispatcher.Invoke(updatePbDelegate,
                        System.Windows.Threading.DispatcherPriority.Background,
                        new object[] { ProgressBar.ValueProperty, value });

                }
                while (progressBar.Value != progressBar.Maximum);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Connect error..Checked again " + ex.Message, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            lbStatic.Content = "Service Stop.....";
            btnStop.IsEnabled = false;
            serviceWCF.StopConnectService();
            btnStart.IsEnabled = true;
            progressBar.Value = 0;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            configConnection.SaveConfig(txtHost.Text, txtDatabase.Text, txtUserName.Text, txtPassword.Text, txtPort.Text, txtServicePort.Text);
        }

        private void btnTestConnect_Click(object sender, RoutedEventArgs e)
        {
            ControlService _new = new ControlService();
            List<UserDto> list = _new.UserGetByAll();
            int flag = configConnection.TestConnect(txtHost.Text, txtDatabase.Text, txtUserName.Text, txtPassword.Text, txtPort.Text);
            if (flag == 0)
            {
                btnStart.IsEnabled = true;
            }
            else
            {
                btnStart.IsEnabled = false;
            }
        }

        private void WindowMain_Loaded(object sender, RoutedEventArgs e)
        {
            configConnection.LoadStaticForm(txtHost, txtDatabase, txtUserName, txtPassword, txtPort, txtServicePort);
            btnStop.IsEnabled = false;
            btnStart.IsEnabled = false;
           
        }
        #endregion


     
    }
}
